package com.example.huayang.appdome.base;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.huayang.appdome.utils.LoadingDialog;


/**
 * Created by WANG on 2017/9/20.
 * Fragment基类
 */

public class BaseFragment extends Fragment{

    private LoadingDialog loadingDialog;//加载动画对话框
    private Bundle b;//fragment之间的切换传值
    public static final int standard = 1;//标准启动模式
    public static final int singleTask = 2;//单例模式
    private View contentView;
    private Intent intent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return container;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        intent = new Intent();
        super.onViewCreated(view, savedInstanceState);
    }

    /**
     * 获取切换fragment传过来的值
     *
     * @return
     */
    public Bundle getBundle() {
        return b;
    }

    /**
     * 切换fragment传值
     *
     * @param b
     */
    public void setBundle(Bundle b) {
        this.b = b;
    }

    /**
     * 镇充布局
     *
     * @param resource 布局ID
     * @return
     */
    public View inflateView(int resource) {
        return inflateView(resource, getActivity().getLayoutInflater());
    }

    /**
     * 镇充布局
     *
     * @param resource 布局ID
     * @return
     */
    public View inflateView(int resource, LayoutInflater inflater) {
        contentView = inflater.inflate(resource, null);
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        return contentView;
    }

    /**
     * fragment启动模式，singleTask和standard两种启动模式
     *
     * @return
     */
    public int registStartMode() {
        return standard;
    }

    /**
     * 按设定的时间长短提示
     *
     * @param content  提示内容
     * @param duration 设定的时间 单位毫秒
     */
    public void showTips(String content, int duration) {
        Toast toast = Toast.makeText(getActivity(), content, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    /**
     * @param content 展示对话框的内容
     */
    public void showDialog(String content) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());//初始化对化框
        }
        loadingDialog.showLoading(content);
    }

    /**
     * 取消对话框
     */
    public void dismiss() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());//初始化对化框
        }
        loadingDialog.dismiss();
    }

    /**
     * 界面跳转且关闭此界面
     *
     * @param cla 要开车去的地方
     */
    public void startAtvAndFinish(Class cla) {
        intent.setClass(getActivity(), cla);
        startActivity(intent);
        getActivity().finish();
    }

    /**
     * 界面跳转不关闭此界面
     *
     * @param cla 要开车去的地方
     */
    public void startAtvDonFinish(Class cla) {
        intent.setClass(getActivity(), cla);
        startActivity(intent);
    }

    /**
     * 界面跳转且获取值
     *
     * @param cla 要开车去的地方
     */
    public void startAtvDonFinishForResult(Class cla,int code) {
        intent.setClass(getActivity(), cla);
        startActivityForResult(intent,code);
    }

}

